#property copyright "Copyright 2012"
#property link      "mingxinhe@gmail.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 1
#property indicator_color2 Red
#property indicator_width2 1


extern double minDif4SameDir.Pips = 0.1;
extern double maxDif4OppsiteDir.Pips = 1;

//---- buffers
double upArrow[];
double downArrow[];

int digitFactor = 1;
datetime lastTime;
bool upArrowSignal = false;
bool downArrowSignal = false;

int  Pointer_Offset = 0;         // The offset value for the arrow to be located off the candle high or low point.
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  //---- 6 additional buffers are used for counting.   
   if(Bid<10 && Digits==5) { digitFactor=10;}
   if(Bid<10 && Digits==4) { digitFactor= 1;}

   if(Bid>10 && Digits==3) { digitFactor=10;}
   if(Bid>10 && Digits==2) { digitFactor= 1;} 
  
   IndicatorBuffers(2);   
//---- drawing settings 
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,5);
   SetIndexBuffer(0, downArrow);
      
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,6);
   SetIndexBuffer(1, upArrow);      
      
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
   lastTime = D'1977.01.01 00:00';      
   
   minDif4SameDir.Pips *= digitFactor;
   maxDif4OppsiteDir.Pips *= digitFactor;
   
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+

int deinit()
{
//---- TODO: add your code here
// --------------------------
//----
   return(0);
}

//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
{  
   double AvgRange;
   if (!isNewBar()) return;  
   
   int  counted_bars=IndicatorCounted();        
   if(counted_bars>0) 
   {
       int limit = Bars - counted_bars + 1;
   }
   else
   {
       limit = Bars - 1;
   }       
      
   while(limit>=0)         
   {                     
       upArrow[limit+1] = 0;
       downArrow[limit+1] = 0;                
       
       AvgRange = 0;       
       for (int counter=limit+1 ;counter<=limit+10;counter++)
       {
           AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
       }
       double Range=AvgRange/10;
                                                                       
       if (((Low[limit + 1] <= Low[limit + 2] - minDif4SameDir.Pips*Point || Low[limit + 1] == Low[limit + 2]) &&
           (Low[limit + 1] <= Low[limit + 3] - minDif4SameDir.Pips*Point || Low[limit + 1] == Low[limit + 3])) ||
           ((Low[limit + 1] <= Low[limit + 2] - minDif4SameDir.Pips*Point || Low[limit + 1] == Low[limit + 2]) &&
            (Low[limit + 1] > Low[limit + 3] && Low[limit + 1] <= Low[limit + 3] + maxDif4OppsiteDir.Pips*Point)) ||            
           ((Low[limit + 1] <= Low[limit + 3] - minDif4SameDir.Pips*Point || Low[limit + 1] == Low[limit + 3]) &&
            (Low[limit + 1] > Low[limit + 2] && Low[limit + 1] <= Low[limit + 2] + maxDif4OppsiteDir.Pips*Point)))
       {
           upArrow[limit+1] = Low[limit+1] - Range*0.3;                      
       }       
       
       if (((High[limit + 1] >= High[limit + 2] + minDif4SameDir.Pips*Point || High[limit + 1] == High[limit + 2]) &&
           (High[limit + 1] >= High[limit + 3] + minDif4SameDir.Pips*Point || High[limit + 1] == High[limit + 3])) ||
           ((High[limit + 1] >= High[limit + 2] + minDif4SameDir.Pips*Point || High[limit + 1] == High[limit + 2]) &&
            (High[limit + 1] < High[limit + 3] && High[limit + 1] >= High[limit + 3] - maxDif4OppsiteDir.Pips*Point)) ||            
           ((High[limit + 1] >= High[limit + 3] + minDif4SameDir.Pips*Point || High[limit + 1] == High[limit + 3]) &&
            (High[limit + 1] < High[limit + 2] && High[limit + 1] >= High[limit + 2] - maxDif4OppsiteDir.Pips*Point)))
       {
           downArrow[limit+1] = High[limit+1] + Range*0.3;                      
       }                                       
      
       limit--;
   }
      
//---- done
   return(0);  
}
  
bool isNewBar()
{
    if (lastTime == Time[0]) return (false);   
    lastTime = Time[0];
    return (true); 
}

 